/**
 * 
 */
package com.dream.house.controller;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.servyou.utils.CoreUtils;
import cn.com.servyou.utils.Page;

import com.dream.house.entity.BuildDTO;
import com.dream.house.entity.BuyDTO;
import com.dream.house.entity.GroupDTO;
import com.dream.house.entity.RentDTO;
import com.dream.house.entity.ReturnDTO;
import com.dream.house.entity.RoomDTO;
import com.dream.house.entity.UserDTO;
import com.dream.house.service.BuildService;
import com.dream.house.service.BuyService;
import com.dream.house.service.RentService;
import com.dream.house.service.ReturnService;
import com.dream.house.service.RoomService;
import com.dream.house.service.UserService;
import com.dream.house.utils.Constants;

/**
 * @author maoq
 *
 */
@Controller
@RequestMapping("return")
public class ReturnController {
	
	@Autowired
	BuildService buildService;
	@Autowired
	UserService userService;
	@Autowired
	RoomService roomService;
	@Autowired
	RentService rentService;
	@Autowired
	BuyService buyService;
	@Autowired
	ReturnService returnService;
	
	@RequestMapping("gototuifangdengji")
	public String gototuifangdengji(Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		List<BuildDTO> buildList = getBuildListByGroupId(groupList.get(0).getGroup_id());
		model.addAttribute("buildList", buildList);
		if(buildList.isEmpty()) {
			model.addAttribute("roomList", Collections.EMPTY_LIST);
		} else {
			model.addAttribute("roomList", getRoomListByBuildId(buildList.get(0).getBuild_id()));
		}
		return "return/tuifang_dengji_chaxun";
	}
	
	private List<BuildDTO> getBuildListByGroupId(String id) {
		BuildDTO dto = new BuildDTO();
		dto.setGroup_id(id);
		dto.setCheck_status(Constants.CHECK_STATUS_PASSED);////
		List<BuildDTO> builds =  buildService.findBuilds(dto);
		return builds;
	}
	
	private List<RoomDTO> getRoomListByBuildId(String id) {
		RoomDTO room = new RoomDTO();
		room.setBuild_id(id);
		room.setCheck_status(Constants.CHECK_STATUS_PASSED);
		return roomService.findRoomsWithBuyRent(room);
	}
	
	@RequestMapping("tuifangdengjichaxun")
	public String tuifangdengjichaxun(Model model, RentDTO dto) {
		dto.setStatus("1");
		BuyDTO buydto = new BuyDTO();
		buydto.setGroup_id(dto.getGroup_id());
		buydto.setBuild_id(dto.getBuild_id());
		buydto.setRoom_id(dto.getRoom_id());
		buydto.setStatus("1");
		List<RentDTO> rent = rentService.findRents(dto);
		if (!rent.isEmpty()) {
			buydto.setBuy_name(rent.get(0).getRent_name());
			buydto.setId_card(rent.get(0).getId_card());
			buydto.setComp_name(rent.get(0).getComp_name());
			buydto.setMate_name(rent.get(0).getMate_name());
			buydto.setMate_card(rent.get(0).getMate_card());
			buydto.setMate_comp(rent.get(0).getMate_comp());
			model.addAttribute("tuifang", buydto);
			return "return/tuifang_dengji";
		}
		List<BuyDTO> buy = buyService.findBuys(buydto);
		if (!buy.isEmpty()){
			model.addAttribute("tuifang", buy.get(0));
			return "return/tuifang_dengji";
		}
		model.addAttribute("tuifang", buydto);
		model.addAttribute("isempty", "1");
		return "return/tuifang_dengji";
	}
	
	@RequestMapping("tuifangdengji")
	public String tuifangdengji(Model model, @ModelAttribute ReturnDTO dto, HttpSession session) {
		try {
			returnService.saveReturn(dto);
			model.addAttribute("error", "1");
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("error", "0");
		}
		return gototuifangdengji(model, session);
	}
	
	@RequestMapping("gototuifangchaxun")
	public String gototuifangchaxun(Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		List<BuildDTO> buildList = getBuildListByGroupId(groupList.get(0).getGroup_id());
		model.addAttribute("buildList", buildList);
		if(buildList.isEmpty()) {
			model.addAttribute("roomList", Collections.EMPTY_LIST);
		} else {
			model.addAttribute("roomList", getRoomListByBuildId(buildList.get(0).getBuild_id()));
		}
		return "return/tuifang_chaxun";
	}
	
	@RequestMapping("tuifangchaxun")
	public String tuifangchaxun(Model model, @ModelAttribute("dto") ReturnDTO dto, int pageNo, int pageSize, String flag) {
		if ("shenpi".equals(flag)) {
			dto.setCheck_status(Constants.CHECK_STATUS_INIT);
		}
		Map<String, Object> condition = CoreUtils.entity2Map(dto);
		Page page = returnService.findByPage(condition, pageNo, pageSize);
		model.addAttribute("page", page);

		if ("shenpi".equals(flag)) {
			return "shenpi/tuifang_list";
		}
		
		return "return/tuifang_list";
	}
	
	@RequestMapping("gotoedit/{id}")
	public String gotoedit(@PathVariable String id, Model model, HttpSession session) {
		ReturnDTO tuifang = returnService.getReturn(id);
		model.addAttribute("tuifang", tuifang);
		return "return/tuifang_edit";
	}
	
	@RequestMapping("doedit")
	public String doedit(@ModelAttribute("tuifang") ReturnDTO tuifang, Model model, HttpSession session) {
		try {
			returnService.updateReturn(tuifang);
			model.addAttribute("error", "1");
		} catch (Exception e) {
			model.addAttribute("error", "0");
		}
		return "return/tuifang_edit";
	}
	
	@ResponseBody
	@RequestMapping("deletetuifang/{id}")
	public String deletebuy(@PathVariable String id, Model model) {
		try {
			returnService.removeReturn(id);
			return "1";
		} catch (Exception e) {
			return "0";
		}
	}
	
}
